package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class StringUtils {
	public static String SafeString(String str) {
		if (str == null) {
			return "";
		} else {
			return str;
		}
	}

	/**
	 * 
	 * @param source
	 *            this line is being splitted into array
	 * @param splitCHar
	 *            this char is being used to split
	 * @return
	 */
	public static String[] splitBy(String source, char splitChar) {

		if (source != null && splitChar == '/') {
			return source.split(String.valueOf(splitChar));
		}
		return null;
	}

	public static boolean isNotNullAndEmpty(String str) {
		boolean notnullAndEmpty = true;
		if (str == null) {
			notnullAndEmpty = false;

		} else if (str.trim().equals("")) {
			notnullAndEmpty = false;
		}
		return notnullAndEmpty;
	}

	public static int getColumnNumber(String colAdress) {
		if (!isNotNullAndEmpty(colAdress)) {
			Integer[] digits = new Integer[colAdress.length()];
			for (int i = 0; i < colAdress.length(); ++i) {
				digits[i] = Integer
						.parseInt(String.valueOf(colAdress.charAt(i))) - 64;
			}
			int mul = 1;
			int res = 0;
			for (int pos = digits.length - 1; pos >= 0; --pos) {
				res += digits[pos] * mul;
				mul *= 26;
			}
			return res;
		} else {
			return -1;
		}

	}

	public static String getColumnAddress(int number) {
		if (number <= 26) {
			return String.valueOf((char) (number + 64));
		}
		int div = number / 26;
		int mod = number % 26;
		if (mod == 0) {
			mod = 26;
			div--;
		}
		return getColumnAddress(div) + getColumnAddress(mod);
	}

	public static boolean isCSVFile(String fp) {
		if (new File(fp).exists() && fp.toLowerCase().endsWith(".csv")) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isXLSXFile(String fp) {
		if (new File(fp).exists() && fp.toLowerCase().endsWith(".xlsx")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * @param inputWord
	 *            the input reserved (or not word)
	 * @return if word is really reserved (java code is mapped for this word)
	 */
	public static boolean isReservedWord(String inputWord) {
		try {
			FileInputStream fis = new FileInputStream(new File(
					"config/mapping.xml"));
			Properties prop = new Properties();
			prop.loadFromXML(fis);
			if (prop.containsKey(inputWord)) {
				return true;
			} else {
				return false;
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}

	}

	public static  String getReservedWordMeaning(String inputWord) {
		if (isReservedWord(inputWord)) {
			try {
				FileInputStream fis = new FileInputStream(new File(
						"config/mapping.xml"));
				Properties prop = new Properties();
				prop.loadFromXML(fis);
				return prop.getProperty(inputWord);
			} catch (Exception ex) {
				ex.printStackTrace();
				return "";
			}
		} else {
			return "";
		}
	}
}
